<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
  <head>
    <base href="<%=basePath%>">
	<%@ include file="/base/jsp/include.jsp"%>
  </head>
  <body>
    <form id="form" method="post">
        <input name="id" class="mini-hidden" />
        <fieldset style="border:solid 1px #aaa;padding:10px;">
            <legend >新闻信息</legend>
            <div style="padding:5px;">
            <table class="editor_table">
            	<tr>
                    <td style="width:200px;">标题：</td>
                    <td style="width:150px;">
                    	<input name="title" id="title" class="mini-textbox editor_len" required="true" vtype="maxLength:50" emptyText="请输入新闻标题"/>
                    </td>
                </tr>
                <tr style="display:none;">
                    <td style="width:200px;">文件ID：</td>
                    <td style="width:150px;">
                    	<input name="img" id="img" class="mini-textbox" required="true" vtype="maxLength:100" emptyText="图片id"/>
                    </td>
                </tr>
                <tr>
                    <td style="width:200px;">描述：</td>
                    <td style="width:150px;">
                    	<textarea name="content" id="content" class="mini-textarea editor_len" required="true" vtype="maxLength:150" emptyText="请输入新闻描述"></textarea>
                    </td>
                </tr>
                <tr>
                    <td style="width:200px;"></td>
                    <td style="width:150px;">
                    	<input type="button" value="预览" onclick="preview()" />
                    </td>
                </tr>
                <tr>
                    <td style="width:100px;">正文：</td>
                    <td style="width:500px;">
                    	<textarea id="editor_id" name="contextEdit" required="true" emptyText="请输入新闻正文" onvaluechanged="editorChange">
						</textarea>
                    </td>
                </tr>
                <tr style="display:none;">
                    <td style="width:70px;">正文字符串：</td>
                    <td style="width:150px;">
                    	<textarea name="contextReal" id="contextReal" class="mini-textarea" required="true"></textarea>
                    	<input name="url" id="url" class="mini-textbox"/>
                    </td>
                </tr>
                <tr>
                    <td style="width:70px;">新闻时间：</td>
                    <td style="width:150px;">
                        <input name="time" id="time" class="mini-datepicker" format="yyyy-MM-dd" value="2014-01-01"/>
                    </td>
                </tr>
                <tr>
                    <td style="width:70px;">选择配图：</td>
                    <td style="width:150px;">
                    	<!--fileCountLimit:上限 multi:是否为多文件 showImage:是否为图片  fileType:文件类型 fileSize:文件大小限制 buttonText:按钮文本 -->
                      <input id="file_upload" fileCountLimit="1" multi="true" type="file" 
                      	showImage="true" onuploadsuccess="onUploadSuccess"  fileType="*.jpg;*.jpeg;*.png" fileSize="1MB" buttonText="上传图标"/>
                    </td>
                </tr>
                <tr>
                    <td style="width:70px;">来源：</td>
                    <td style="width:150px;">
                     <input name="source" id="source"" class="mini-textbox editor_len" required="true" vtype="maxLength:50" emptyText="请输入新闻来源"/> 
                    </td>
                </tr>
            </table>            
         </div>
        </fieldset>
        
        <div style="text-align:center;padding:10px;">
            <a class="mini-button" onclick="onOk" style="width:60px;margin-right:20px;">确定</a>       
            <a class="mini-button" onclick="onCancel" style="width:60px;">取消</a>       
        </div>        
    </form>
    <script type="text/javascript" src="${path}/base/js/kindeditor_use.js"></script>
    <script type="text/javascript">
    	var id = '';
        mini.parse();
        var form = new mini.Form("form");
        function SaveData() {
        	editor.sync();
        	
			html = $('#editor_id').val(); // jQuery
			var webContent = $("<html></html>");
			webContent.append(html);
			var imgV = webContent.find('img');
			for(var i = 0, len = imgV.length; i < len; i++) {
				imgV.eq(i).attr("style", "");
			}
			mini.get("contextReal").setValue(webContent.html());
			var o = form.getData();
            form.validate();
            if (form.isValid() == false) return;
            var json = mini.encode(o);
   
            $.ajax({
                url: "saveNewsImg.do",
                type: 'post',
                data: $.evalJSON(json),
                cache: false,
                success: function (text) {
                	if(text.indexOf('true') > -1){
				    	mini.alert("操作成功");
				    	CloseWindow("save");
				    }else{
				    	mini.alert("操作失败");
				    	CloseWindow();
				    }
                },
                error: function (textStatus, errorThrown) {
                    alert(jqXHR.responseText);
                    CloseWindow();
                }
            });
        }
        
        //标准方法接口定义
        function SetData(data) {
       		editor.sync();
			html = $('#editor_id').val(); // jQuery
			mini.get("contextReal").setValue(html);
            if (data.action == "edit") {
                //跨页面传递的数据对象，克隆后才可以安全使用
                data = mini.clone(data);
                $.ajax({
                    url: "findByIdNewsImg.do?id=" + data.id,
                    cache: false,
                    success: function (text) {
                        var o = mini.decode(text);
                        form.setData(o);
                        form.setChanged(false);
                        id = o.id;

                        $.ajax({
                    		url: "pageContent.do?url=" + o.url,
                    		cache: false,
                    		success: function (text) {
                        		editor.html(text);
                        		
                   	 		}
                		});
                        var imgs = mini.get("img").getValue();
                        if(imgs != ''){
               			 $('#file_upload').setFileId(imgs,true,true);
               		    }
                    }
                });
            }
        }

        function GetData() {
            var o = form.getData();
            return o;
        }
        function CloseWindow(action) {
            if (action == "cancel" && form.isChanged()) {
                if (confirm("数据被修改了，是否先保存？")) {
                	SaveData();
                	return false;
                }
            }
            if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
            else window.close();            
        }
        function onOk(e) {
        	var imgId = mini.get("img").getValue();
        	if(imgId == ""){
        		mini.alert("请上传图片");
        		return ;
        	}
            SaveData();
        }
        function onCancel(e) {
            CloseWindow("cancel");
        }
        
    </script>
  </body>
</html>
